﻿using AutoMapper;
using Sustainalytics.SharedEntities.Corporate;
using Sustainalytics.UrlHelpers;
using System;
using ResearchState = Sustainalytics.BusinessResearchProductStates.Entities.ResearchState;

namespace Sustainalytics.ClientDW.Synchronization.SyncHbaseData
{
    public static class AutoMapperConfig
    {
        private static readonly UrlFriendlyGenerator urlFriendlyNameGenerator = new UrlFriendlyGenerator();
        
        public static void RegisterMappings()
        {
            Mapper.Reset();
            Mapper.Initialize(Config);
        }

        private static void Config(IMapperConfigurationExpression cfg)
        {
            cfg.AllowNullCollections = true;
            cfg.AllowNullDestinationValues = true;

            cfg.CreateMap<CorporateDetails, Entities.CorporateData>()
                .ForMember(cd => cd.CapitalIqId, opt => opt.MapFrom(cddb => cddb.MainIdentifiers.CompanyExternalId))
                .ForMember(cd => cd.MainExchangeTicker, opt => opt.MapFrom(cddb => cddb.MainIdentifiers.ExchangeTicker))
                .ForMember(cd => cd.MainExchange, opt => opt.MapFrom(cddb => GetExchangeTickerSegment(cddb.MainIdentifiers.ExchangeTicker, true)))
                .ForMember(cd => cd.MainTicker, opt => opt.MapFrom(cddb => GetExchangeTickerSegment(cddb.MainIdentifiers.ExchangeTicker, false)))
                .ForMember(cd => cd.CompanyNameLowercase, opt => opt.MapFrom(cddb => cddb.CompanyName.ToLowerInvariant()))
                .ForMember(cd => cd.CompanyNameUrlFriendly, opt => opt.MapFrom(cddb => urlFriendlyNameGenerator.GetFriendlyNameById(cddb.Id, cddb.CompanyName)))
                .ForMember(cd => cd.TemplateType, opt => opt.MapFrom(cddb => "Type " + cddb.TemplateType))
                .ForMember(cd => cd.UniverseIds, opt => opt.Ignore())
                .ForMember(cd => cd.LastControversyUpdate, opt => opt.Ignore());

            cfg.CreateMap<CorporateDetails, ResearchState.CompanyProfileResearchState>()
                .ForMember(cprs => cprs.CapitalIqId, opt => opt.MapFrom(cddb => cddb.MainIdentifiers.CompanyExternalId))
                .ForMember(cprs => cprs.UniverseIds, opt => opt.Ignore())
                .ForMember(cprs => cprs.SecurityListIds, opt => opt.Ignore());

            cfg.CreateMap<CorporateDetails, Entities.FinancialData>()
                .ForMember(x => x.UniverseIds, opt => opt.Ignore())
                .ForMember(x => x.NEBT, opt => opt.MapFrom(x => x.EBIT));

            cfg.CreateMap<Timestamps, Entities.CorporateDataTimestamps>();

            cfg.CreateMap<Timestamps, Entities.FinancialDataTimestamps>();

            cfg.CreateMap<PUResearch, ResearchState.PUResearch>()
                .ForMember(x => x.HasResearch, opt => opt.MapFrom(y => (int)y.ResearchState == (int)ResearchState.ResearchState.ResearchAvailable));

            cfg.CreateMap<MainIdentifiers, Entities.MainIdentifiers>();


        }

        private static string GetExchangeTickerSegment(string exchangeTickerComposite, bool getExchangeVsTicker)
        {
            var segmentIndex = getExchangeVsTicker ? 0 : 1;

            var mainExchangeTickerSegments = exchangeTickerComposite?.Split(':');

            return mainExchangeTickerSegments?.Length == 2 ? mainExchangeTickerSegments[segmentIndex] : null;
        }
    }
}
